package com.youlu.campus.web.community.controller;

import com.youlu.campus.entity.OrderInfo;
import com.youlu.campus.entity.UniversityInfo;
import com.youlu.campus.entity.VO.req.PlaceOrderVO;
import com.youlu.campus.entity.VO.req.UserCaQuery;
import com.youlu.campus.entity.domain.ActivityRewadDomain;
import com.youlu.campus.web.common.base.RequestContext;
import com.youlu.campus.web.community.service.WechatMiniCaService;
import com.youlu.campus.web.university.service.UniversityService;
import com.youlu.campus.web.common.utils.CommonUtils;
import com.yuelin.infrastructure.quantum.common.QResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.Objects;

/**
 * 微信小程序证书控制器
 */
@RestController
public class WechatMiniCaController {
    @Value("${sys.pay.wx.payWechatNotifyUrl}")
    private String notifyUrl;
    @Autowired
    private WechatMiniCaService wechatMiniCaService;
    @Autowired
    private UniversityService universityService;

    /**
     * 获取多个纸质版证书领取信息
     *
     * @return
     */
    @PostMapping("/mini-app/ca/mini-recieve-info")
    public QResult getCaInfos(@NotNull @RequestBody UserCaQuery req) {
        req.setUserId(RequestContext.getUserId());
        req.setCurrentUser(RequestContext.getCurUser());
        return QResult.success(wechatMiniCaService.getUserCaInfos(req));
    }

    /**
     * 获取订单纸质版证书综合信息
     *
     * @return
     */
    @PostMapping("/mini-app/ca/mini-collect-info")
    public QResult getActivityOrderItemInfo(@NotNull @RequestBody UserCaQuery req) {
        req.setUserId(RequestContext.getUserId());
        req.setCurrentUser(RequestContext.getCurUser());
        return QResult.success(wechatMiniCaService.getActivtyOrderItemInfo(req));
    }

    /**
     * 下单
     *
     * @return
     */
    @PostMapping("/mini-app/ca/mini-place-order")
    public QResult payOrder(@NotNull @RequestBody PlaceOrderVO req) {
        req.setPlatform(RequestContext.getCurPlatform());
        req.setUserId(RequestContext.getUserId());
        return QResult.success(wechatMiniCaService.placeOrder(req,RequestContext.getCurUser()));
    }

    /**
     * 下单
     *
     * @return
     */
    @PostMapping("/mini-app/ca/mini-replace-order")
    public QResult payOrder(@NotNull @RequestBody OrderInfo req) {
        return QResult.success(wechatMiniCaService.updateOrder(req,RequestContext.getCurUser(),RequestContext.getCurPlatform()));
    }

    /**
     *支付
     * @param req
     * @param request
     * @return
     */
    @PostMapping("/mini-app/ca/mini-pay-order")
    public QResult payOrder(@RequestBody ActivityRewadDomain req, HttpServletRequest request) {
        req.setUserId(RequestContext.getUserId());
        req.setClientIp(CommonUtils.getRemoteIp(request));
        req.setSource(1);
        req.setUserPO(RequestContext.getCurUser());
        req.setPlatfrom(RequestContext.getCurPlatform());
        req.setUrl(notifyUrl);
        if (StringUtils.isBlank(req.getUniversityId())) {
            UniversityInfo universityInfo = universityService.getUniversity(RequestContext.getCurUser(), req.getMobile());
            if (Objects.nonNull(universityInfo)) {
                req.setUniversityId(universityInfo.getId());
            }
        }
        return QResult.success(wechatMiniCaService.payOrder(req, RequestContext.getCurUser()));
    }

    @GetMapping("/mini-app/ca/mini-order-list")
    public QResult orderList(@NotNull @RequestParam("activityId") String activityId){
        return QResult.success(wechatMiniCaService.getOrders(activityId));
    }


}
